#!/bin/bash


function docker-build(){
    if [ "$dockerImageVersion" == "" ]; then
        echo "!!!!!!!!!!!!!!!!!!!!!!!!"
        echo "dockerImageVersion 变量值为空!"
        echo "!!!!!!!!!!!!!!!!!!!!!!!!"
        return 1
    fi


    # 执行编译前、编译、编译后
    docker-compile

    if [ "$isLib" == "true" ]; then
      return
    fi

    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "构建命令:" `pwd`

    if [ "$language" == "java" ]; then
      echo "整理 code/$folder/target 结构,构造 target-jar !"
      mkdir  -p code/$folder/target-jar

      mkdir -p code/$folder/target/lib
      find code/$folder/target/lib -exec touch -t 202001010000.00 {} \;
      tar -cf code/$folder/lib.tar -C code/$folder/target/lib  .
      touch -t 202001010000.00 code/$folder/lib.tar

      mv code/$folder/target/*.jar  code/$folder/target-jar

      if [ $(ls -1 code/$folder/target | grep  "\.txt$" | wc -l) -gt 0 ] ; then
        mv code/$folder/target/*.txt  code/$folder/target-jar
      fi

      if [ -f "start-java-${languageVersion}.sh" ]; then
         cp start-java-${languageVersion}.sh   code/$folder/target-jar/start-java.sh
      else
         cp start-java.sh   code/$folder/target-jar
      fi

      cp  code/$folder/ops-report.txt code/$folder/target-jar

    fi

    echo "${sudo} docker build  $env_dockerBuildAddHostsParameters  -t ${imageTagPrefix}$serviceName:$dockerImageVersion  ."
    ${sudo} docker build  $env_dockerBuildAddHostsParameters  -t ${imageTagPrefix}$serviceName:$dockerImageVersion  .


    if [ $? -ne 0 ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '构建镜像错误!!!'
        return 1
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '构建镜像成功!'
    fi

}


function docker-compile(){
    mount_path=""
    if [ "$language" == "java" ]; then
      mount_path=" -v /data/mvn-repo:/root/.m2 -v `getPwdInDockerPath`/code/maven.setting.xml:/usr/share/maven/conf/settings.xml "
    fi

    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "编译命令:"


    echo "${sudo} docker run --rm   --privileged=true $env_dockerBuildAddHostsParameters \
        -v `getPwdInDockerPath`/code:/code  \
        $mount_path  \
        $env_dockerBuildImage bash -c 'TZ="Asia/Shanghai"; source /code/docker-build.sh ; build '"

    ${sudo} docker run --rm  --privileged=true $env_dockerBuildAddHostsParameters \
    -v `getPwdInDockerPath`/code:/code  \
    $mount_path  \
    $env_dockerBuildImage bash -c 'TZ="Asia/Shanghai" ; source /code/docker-build.sh ; build'



    if [ $? -ne 0 ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '编译错误!!!'
        return 1
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '编译成功!'
    fi
}

function docker-login(){
  if [ -n "$env_harborHost" ] && [ -n "$env_harborUser" ] && [ -n "$env_harborPassword" ] ; then
    echo "登录到 harbor: $env_harborHost"
    echo "${sudo} docker login -u $env_harborUser -p $env_harborPassword $env_harborHost"
    ${sudo} docker login -u $env_harborUser -p $env_harborPassword $env_harborHost


    if [ $? -ne 0 ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` 'harbor 登录错误!!!'
        exit 1
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` 'harbor 登录成功!'
    fi
  fi
}

function docker-push() {
  if [ -z "$env_harborHost" ]; then
    return
  fi

  docker-login

  echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "推送镜像:"

  echo "${sudo} docker push ${imageTagPrefix}$serviceName:$dockerImageVersion"
  ${sudo} docker push ${imageTagPrefix}$serviceName:$dockerImageVersion


  if [ $? -ne 0 ]; then
      echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '上传镜像错误!!!'
      return 1
  else
      echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '上传镜像成功!'
  fi


  echo "清理docker镜像"
  ${sudo} docker rmi ${imageTagPrefix}$serviceName:$dockerImageVersion
}


function check-docker-status(){
  :
}